﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HandMatchOrder.aspx.cs" Inherits="UI_Orders_HandMatchOrder" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>手动匹配页面</title>
    <link href="../../css/admin.css" rel="stylesheet" type="text/css" />
    <script src="../../js/common.js" type="text/javascript"></script>
    <link href="../../js/jquery.easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../js/jquery.easyui/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../js/jquery.easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    
<script type="text/javascript">
    $(function () {
        $("#ddloebayUserId").combobox({
            editable: false
        });
        $("#ddlpebayUserId").combobox({
            editable: false
        });
        var defaultDate = Date.parse('1900/01/01');

        var _$order = $("#order");
        _$order.datagrid({
            border: true,
            height: 330,
            width: 1100,
            title: '订单列表...',
            remoteSort: true,
            nowrap: true,
            striped: false,
            singleSelect: false,
           // url: "../../TableHanders.aspx?act=q&tid=1014&type=0",
            sortName: 'OrderId',
            sortOrder: 'asc',
            loadMsg: "正在加载,请稍后...",
            columns: [[
	                { field: 'ck', width: 30, checkbox: true },
	                { title: 'EbayUserId', field: 'EbayUserId', width: 85, sortable: true },
	                { title: 'OrderId', field: 'OrderId', width: 180, sortable: true, formatter: function (val, rec) {
	                    if (rec.SellingStatus == '2') {
	                        return '<span style="color:red;">' + val + '</span>';
	                    }
	                    else {
	                        return val;
	                    }
	                }
	                },
                    { title: 'Rec_Num', field: 'OrderSalesRecordNumber', width: 100, sortable: true },
	                { title: 'BuyerId', field: 'UserId', width: 120, sortable: true },
	                { title: 'BuyerFullName', field: 'BuyerFullName', width: 200, sortable: true },
	                { title: 'Quantity', field: 'Quantity', width: 60, sortable: true, align: 'right' },
	                { title: 'SalePrice', field: 'SalePrice', width: 60, sortable: true, align: 'right' },
                    { title: 'PostageAndPackaging', field: 'PostageAndPackaging', width: 120, sortable: true, align: 'right' },
	                { title: 'TotalSalePrice', field: 'TotalSalePrice', width: 60, sortable: true, align: 'right' },
                    { title: 'PaymentOrRefundAmount', field: 'PaymentOrRefundAmount', width: 60, sortable: true, align: 'right' },
	                { title: 'BuyerEmail', field: 'BuyerEmail', width: 200, sortable: true },
	                { title: 'BuyerAddress1', field: 'BuyerAddress1', width: 200, sortable: true },
	                { title: 'BuyerAddress2', field: 'BuyerAddress2', width: 200, sortable: true },
	                { title: 'BuyerCity', field: 'BuyerCity', width: 120, sortable: true },
	                { title: 'BuyerPostcode', field: 'BuyerPostcode', width: 80, sortable: true },
	                { title: 'BuyerCountry', field: 'BuyerCountry', width: 80, sortable: true },
	                { title: 'PayPalTransactionID', field: 'PayPalTransactionID', width: 130, sortable: true },
                    { field: 'SellingStatus', title: '状态', width: 50, sortable: true, formatter: function (val, rec) {
                        if (val == '2') {
                            return '<span style="color:red;">已匹配</span>';
                        }
                        else {
                            return '未匹配';
                        }
                    }
                    }
				]],
            pagination: true,
            rownumbers: true,
            pageList: [10, 15, 20],
            queryParams: {
                "timeFrom": $("#obeginDate").datebox('getValue'),
                "timeTo": $("#oendDate").datebox('getValue'),
                "EbayUserId": "",
                "SellingStatus": "",
                "OrderId": "",
                "BuyerId": "",
                "BuyerName": "",
                "buyerEmail": "",
                "recNum": ""
            },
            onBeforeLoad: function () {
                clickRowIndex = -1;
            },
            onLoadError: function () {
                alert('数据加载失败!');
            }
        });
        $("#btnQuery").click(function () {
            var timeFrom = $("#obeginDate").datebox('getValue');
            var timeTo = $("#oendDate").datebox('getValue');
            var EbayUserId = $("#ddloebayUserId").combobox('getValue');
            var SellingStatus = $("#ddlSellingStatus").combobox('getValue');
            var orderId = $("#txtOrderId").val();
            var BuyerId = $("#txtoBuyerId").val();
            var BuyerName = $("#txtoBuyerName").val();
            var buyerEmail = $("#txtBuyerEmail").val();
            var recNum = $("#txtRec_Num").val();
            _$order.datagrid({
                url: "../../TableHanders.aspx?act=q&tid=1014&type=0",
                queryParams: {
                    "timeFrom": timeFrom,
                    "timeTo": timeTo,
                    "EbayUserId": EbayUserId,
                    "SellingStatus": SellingStatus,
                    "OrderId": orderId,
                    "BuyerId": BuyerId,
                    "BuyerName": BuyerName,
                    "buyerEmail": buyerEmail,
                    "recNum": recNum
                }
            });
        });
        var _$pp = $("#pp");
        _$pp.datagrid({
            border: true,
            height: 330,
            width: 1100,
            title: 'PayPal列表...',
            remoteSort: true,
            nowrap: true,
            striped: false,
            singleSelect: false, //修改后
            //url: "../../TableHanders.aspx?act=q&tid=1015&type=0",
            sortName: 'TransactionId',
            sortOrder: 'asc',
            loadMsg: "正在加载,请稍后...",
            frozenColumns: [[
	            { field: 'ck', width: 30, checkbox: true },
	            { title: 'EbayUserId', field: 'EbayUserId', width: 120, sortable: true },
	            { title: 'TransactionId', field: 'TransactionId', width: 130, sortable: true, formatter: function (val, rec) {
	                if (rec.StatusId == '2') {
	                    return '<span style="color:red;">' + val + '</span>';
	                }
	                else if (rec.StatusId == '3') {
	                    return '<span style="color:blue;">' + val + '</span>';
	                }
	                else {
	                    return val;
	                }
	            }
	            }
				]],
            columns: [[
	                { title: '类别', field: 'CategoryName', width: 80, sortable: true },
	                { title: 'DateTime', field: 'CreateDateTime', width: 130, sortable: true },
	                { title: 'TimeZone', field: 'TimeZone', width: 80, sortable: true },
	                { title: 'Name', field: 'Name', width: 150, sortable: true },
	                { title: 'Type', field: 'Type', width: 200, sortable: true },
	                { title: 'Status', field: 'Status', width: 120, sortable: true },
	                { title: 'Subject', field: 'Subject', width: 80, sortable: true },
	                { title: 'Currency', field: 'Currency', width: 80, sortable: true },
	                { title: 'Gross', field: 'Gross', width: 80, sortable: true, align: 'right' },
	                { title: 'Fee', field: 'Fee', width: 80, sortable: true, align: 'right' },
	                { title: 'Net', field: 'Net', width: 80, sortable: true, align: 'right' },
	                { title: 'Note', field: 'Note', width: 80, sortable: true },
	                { title: 'FromEmailAddress', field: 'FromEmailAddress', width: 200, sortable: true },
	                { title: 'ToEmailAddress', field: 'ToEmailAddress', width: 200, sortable: true },
	                { title: 'PaymentType', field: 'PaymentType', width: 80, sortable: true },
	                { title: 'CounterpartyStatus', field: 'CounterpartyStatus', width: 120, sortable: true },
	                { title: 'ShippingAddress', field: 'ShippingAddress', width: 120, sortable: true },
	                { title: 'AddressStatus', field: 'AddressStatus', width: 120, sortable: true },
					{ field: 'PostageandPackingAmount', title: 'PostageandPackingAmount', width: 160, sortable: true, align: 'right' },
					{ field: 'InsuranceAmount', title: 'InsuranceAmount', width: 120, sortable: true, align: 'right' },
					{ field: 'VAT', title: 'VAT', width: 85, sortable: true, align: 'right' },
					{ field: 'AuctionSite', title: 'AuctionSite', width: 85, sortable: true },
					{ field: 'BuyerID', title: 'BuyerID', width: 130, sortable: true },
            		{ field: 'ClosingDate', title: 'ClosingDate', width: 130, sortable: true, formatter: function (val) {
            		    var defaultDate = Date.parse('1900/01/01');
            		    var d = Date.parse(val.replace("-", "/"));
            		    if (d == defaultDate) {
            		        return "";
            		    }
            		    else {
            		        return val;
            		    }
            		}
            		},
					{ field: 'ReferenceTxnID', title: 'ReferenceTxnID', width: 130, sortable: true },
					{ field: 'InvoiceNumber', title: 'InvoiceNumber', width: 125, sortable: true },
					{ field: 'SubscriptionNumber', title: 'SubscriptionNumber', width: 140, sortable: true },
					{ field: 'CustomNumber', title: 'CustomNumber', width: 100, sortable: true },
					{ field: 'ReceiptID', title: 'ReceiptID', width: 85, sortable: true },
					{ field: 'Balance', title: 'Balance', width: 85, sortable: true, align: 'right' },
					{ field: 'AddressLine1', title: 'AddressLine1', width: 200, sortable: true },
					{ field: 'AddressLine2', title: 'AddressLine2', width: 200, sortable: true },
					{ field: 'City', title: 'City', width: 85, sortable: true },
					{ field: 'County', title: 'County', width: 150, sortable: true },
					{ field: 'PostalCode', title: 'PostalCode', width: 85, sortable: true },
					{ field: 'Country', title: 'Country', width: 130, sortable: true },
					{ field: 'ContactPhoneNumber', title: 'ContactPhoneNumber', width: 140, sortable: true },
					{ field: 'BalanceImpact', title: 'BalanceImpact', width: 140, sortable: true },
					{ field: 'ToInvoice', title: '已发货', width: 80, sortable: true, formatter: function (val, rec) {
					    if (val == 'Y') {
					        return '是';
					    }
					    else {
					        return '否';
					    }
					}
					},
					{ field: 'ToPz', title: '写入凭证', width: 80, sortable: true, formatter: function (val, rec) {
					    if (val == 'Y') {
					        return '是';
					    }
					    else {
					        return '否';
					    }
					}
					},
					{ field: 'StatusId', title: '状态', width: 50, sortable: true, formatter: function (val, rec) {
					    if (val == '2') {
					        return '<span style="color:red;">已匹配</span>';
					    }
					    else if (val == '3') {
					        return '<span style="color:blue;">手动取消</span>';
					    }
					    else {
					        return '未匹配';
					    }
					}
					}
				]],
            pagination: true,
            rownumbers: true,
            pageList: [10, 15, 20],
            queryParams: {
                "timeFrom": $("#obeginDate").datebox('getValue'),
                "timeTo": $("#oendDate").datebox('getValue'),
                "EbayUserId": "",
                "StatusId": "",
                "transId": "",
                "BuyerId": "",
                "BuyerName": "",
                "flag": "0",
                "FromEmailAddress": "",
                "ToEmailAddress": ""
            },
            onBeforeLoad: function () {
                clickRowIndex = -1;
            },
            onLoadError: function () {
                alert('数据加载失败!');
            }
        });
        $("#btnpQuery").click(function () {
            var timeFrom = $("#pbeginDate").datebox('getValue');
            var timeTo = $("#pendDate").datebox('getValue');
            var EbayUserId = $("#ddlpebayUserId").combobox('getValue');
            var StatusId = $("#ddlStatusId").combobox('getValue');
            var transId = $("#txttransId").val();
            var BuyerId = $("#txtpBuyerId").val();
            var BuyerName = $("#txtpBuyerName").val();
            var FromEmailAddress = $("#txtFromEmailAddress").val();
            var ToEmailAddress = $("#txtToEmailAddress").val();
            var flag = "0";
            if ($("#chkerror").attr("checked")) {
                flag = "1";
            }
            _$pp.datagrid({
                url: "../../TableHanders.aspx?act=q&tid=1015&type=0",
                queryParams: {
                    "timeFrom": timeFrom,
                    "timeTo": timeTo,
                    "EbayUserId": EbayUserId,
                    "StatusId": StatusId,
                    "transId": transId,
                    "BuyerId": BuyerId,
                    "BuyerName": BuyerName,
                    "flag": flag,
                    "FromEmailAddress": FromEmailAddress,
                    "ToEmailAddress": ToEmailAddress
                }
            });
        });

        //匹配
        $("#btnmatch").click(function () {
            var orows = _$order.datagrid('getSelections');
            var prows = _$pp.datagrid('getSelections');
            if (orows.length == 0) {
                alert('请选择一个订单');
                return;
            }
//            if (orows.length > 1) {
//                alert('只能选择一条订单');
//                return;
//            }
            if (prows.length == 0) {
                alert('请选择一个PayPal订单');
                return;
            }
            if (prows.length > 1) {
                if (orows.length > 1) {
                    alert('PayPal当前只能选择一条');
                    return;
                }
            }
            var oids = "";
            var buyerId = orows[0].UserId;
            var ebayuserid = orows[0].EbayUserId;

            //修改：去掉限制2012-8-23
            if (ebayuserid != prows[0].EbayUserId) {
                if (!confirm('Ebay帐号不同确认要匹配?..'))
                    return;
            }
            else {
                if (!confirm('确认匹配?'))
                    return;
            }
            for (i = 0; i < orows.length; i++) {
                var lbuyerid = orows[i].UserId;
                if (lbuyerid != buyerId) {
                    alert('所选多笔订单必须是同一个人');
                    return;
                }
                oids += orows[i].EbayUserId + ":" + orows[i].OrderId + ",";
            }
            var transId = "";
            transId = prows[0].TransactionId;
            oids = oids.substring(0, oids.length - 1);
           
            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=handmatch&oids=" + oids + "&transactionId=" + transId + "&ts=" + new Date().getTime();
            $.getJSON(url, function (data) {
                var retData = EvalData(data);
                var code = retData.retCode;
                if (code == 0) {
                    alert('操作成功!');
                    _$order.datagrid('reload');
                    _$pp.datagrid('reload');
                }
                else {
                    alert(retData.retMsg);
                }
            });
        });

        //关联
        $("#btnrelation").click(function () {
            var orows = _$order.datagrid('getSelections');
            var prows = _$pp.datagrid('getSelections');
            if (orows.length == 0) {
                alert('请选择一个订单');
                return;
            }
            if (prows.length == 0) {
                alert('请选择一个PayPal订单');
                return;
            }
            if (orows.length > 1) {
                alert('Order当前只能选择一条');
                return;
            }
            if (prows.length > 1) {
                alert('PayPal记录只能选择一条');
                return;
            }
            if (orows[0].SellingStatus != 2) {
                alert('订单匹配状态必须为已匹配');
                return;
            }
            if (prows[0].StatusId != 1) {
                alert('paypal匹配状态必须为未匹配');
                return;
            }
            var oid = "";
            var buyerId = orows[0].UserId;
            var pbuyerId = prows[0].BuyerID;
            var ebayuserid = orows[0].EbayUserId;
            var orderLineItemId = orows[0].OrderLineItemId;
            var pebayuserid = prows[0].EbayUserId;
//            if (ebayuserid != pebayuserid) {
//                alert('ebay帐号必须相同');
//                return;
//            }
            var tsId = prows[0].TransactionId;
            oid = orows[0].OrderId;           
            if (!confirm('确认关联?'))
                return;
            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=relation&oid=" + oid + "&ebayuserid=" + ebayuserid + "&tsId=" + tsId + "&ts=" + new Date().getTime();
            $.getJSON(url, function (data) {
                var retData = EvalData(data);
                var code = retData.retCode;
                if (code == 0) {
                    alert('操作成功!');
                    _$order.datagrid('reload');
                    _$pp.datagrid('reload');
                }
                else {
                    alert(retData.retMsg);
                }
            });
        });
        //不能匹配标记
        $("#btnCancelMatch").click(function () {
            var prows = _$pp.datagrid('getSelections');
            if (prows.length == 0) {
                alert('请至少选择一个PayPal订单');
                return;
            }
            var pids = "";
            for (i = 0; i < prows.length; i++) {
                var stId = prows[i].StatusId;
                if (stId == 2) {
                    alert('已匹配的订单不能手动取消');
                    return;
                }
                pids += prows[i].TransactionId + ":" + prows[i].StatusId + ",";
            }
            pids = pids.substring(0, pids.length - 1);
            if (!confirm('确认要做此操作吗？..'))
                return;
            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=PPCancelMatch&o&pids=" + pids + "&ts=" + new Date().getTime();
            $.getJSON(url, function (data) {
                var retData = EvalData(data);
                var code = retData.retCode;
                if (code == 0) {
                    alert('操作成功!');
                    _$pp.datagrid('reload');
                }
                else {
                    alert(retData.retMsg);
                }
            });
        });

        $('#obeginDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });

        $('#oendDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });

        $('#pbeginDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });
        $('#pendDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });
    });
</script>
</head>
<body>
    <form id= "fomrquery" runat="server">
    <div class="div_Title">
        <span class="Body_Title">》》<%=this.Title %></span>
    </div>    
    <div class = "easyui-panel" title="Order查询区..." style="width:1100px;">
        <table>
            <tr>
                <td>bay帐号:</td>
                <td>
            	    <select  id="ddloebayUserId" runat="server" class="easyui-combobox" style="width:206px;" ></select></td>
                <td>起始日期:</td>
                <td>
                    <input id="obeginDate" type="text" class ="easyui-datetimebox" runat="server"  required="true" style="width:206px"/>
                </td>
                <td> 结束日期 </td>
                <td>
                    <input id="oendDate" type="text" class ="easyui-datetimebox" runat="server"  required="true" style="width:206px"/>
                </td>
                <td rowspan="3">匹配状态:</td>
                <td rowspan="3">
                    <select  id="ddlSellingStatus" runat="server" class="easyui-combobox" style="width:85px;" ></select>
                </td>
                <td rowspan="3">
                    <a id="btnQuery" href="#" class="easyui-linkbutton" icon="icon-search">Query</a>
                </td>
            </tr>
            <tr>
                <td>订单号:</td>
                <td>
            	    <input id="txtOrderId" runat="server" style="width:200px;" /></td>
                <td>BuyerId:</td>
                <td>
            	    <input id="txtoBuyerId" runat="server" style="width:200px;" /></td>
                <td> BuyerName:</td>
                <td>
            	    <input id="txtoBuyerName" runat="server" style="width:200px;" /></td>
            </tr>
              <tr>                
                <td> BuyerEmail:</td>
                <td>
            	    <input id="txtBuyerEmail" runat="server" style="width:200px;" /></td>
                <td> Rec_Num:</td>
                <td>
            	    <input id="txtRec_Num" runat="server" style="width:200px;" /></td>
            </tr>

        </table>
    </div>
    
    <div class = "easyui-panel" title="PayPal查询区..." style="width:1100px;">
        <table>
            <tr>
                <td>bay帐号:</td>
                <td>
        	        <select  id="ddlpebayUserId" runat="server" class="easyui-combobox" style="width:206px;" ></select></td>
                <td>起始日期:</td>
                <td>
                    <input id="pbeginDate" type="text" class ="easyui-datetimebox" runat="server"  required="true" style="width:206px"/>
                </td>
                <td> 结束日期 </td>
                <td>
                    <input id="pendDate" type="text" class ="easyui-datetimebox" runat="server"  required="true" style="width:206px"/>
                </td>
                <td rowspan="3">匹配状态:</td>
                <td rowspan="3">
                    <select  id="ddlStatusId" runat="server" class="easyui-combobox" style="width:85px;" ></select>
                </td>
                <td rowspan="3">
                    <a id="btnpQuery" href="#" class="easyui-linkbutton" icon="icon-search">Query</a>
                </td>
            </tr>
            <tr>
                <td>交易号:</td>
                <td>
        	        <input id="txttransId" runat="server" style="width:200px;" /></td>
                <td>BuyerId:</td>
                <td>
        	        <input id="txtpBuyerId" runat="server" style="width:200px;" /></td>
                <td> BuyerName:</td>
                <td><input id="txtpBuyerName" runat="server" style="width:200px;" /></td>               
            </tr>
             <tr>
                <td>FromEmail:</td>
                <td>
        	        <input id="txtFromEmailAddress" runat="server" style="width:200px;" /></td>
                <td>ToEmail:</td>
                <td>
        	        <input id="txtToEmailAddress" runat="server" style="width:200px;" /></td>   
                <td style = "vertical-align:middle;" colspan = "2"><label for="chkerror">筛选可匹配</label><input id="chkerror" type="checkbox" name="chkerror" /></td>            
            </tr>
        </table>
    </div>
    <table id="order"></table>
    <table id="pp"></table>    
    <a id="btnmatch" href="#" class="easyui-linkbutton" >匹配</a>
    <a id="btnrelation" href="#" class="easyui-linkbutton" >关联</a>
    <a id="btnCancelMatch" href="#" class="easyui-linkbutton" >手动取消</a>
    </form>
</body>
</html>
